/*
 * ChatUserInfoManager.java
 *
 * Created on 2007年5月17日, 上午10:43
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.pphome.orgmodel.manager;

import com.ecomm.chat.manager.ChatUserInfoManager;
import com.ecomm.chat.pojo.ChatUserInfo;
import com.ecomm.platcore.service.BaseEntityManager;
import com.ecomm.util.DB;
import com.ecomm.util.DBFactory;
import com.ecomm.util.HibernateUtil;
import com.ecomm.util.SpringUtil;
import com.pphome.orgmodel.pojo.OrgInfo;
import com.pphome.orgmodel.pojo.UserInfo;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Session;


/**
 *
 * <p>Title: 易讯聊天系统</p>
 * <p>Description:
 *  易讯聊天系统
 * </p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: 联合易讯 </p>
 * @author <font color='blue'>pijunliang</font> &nbsp&nbsp<a href="mailto:pijunliang@hotmail.com">pijunliang@hotmail.com</a>
 * @version 1.0
 * @since 2007年5月17日, 上午10:43
 */
public class UserManager extends BaseEntityManager<UserInfo> {
  
  /** Creates a new instance of ChatRemindManager */
  public UserManager() {
    
  }
  private static UserManager userManager = null;
  
  public static UserManager getInstance(){
    if(userManager == null){
      userManager = (UserManager)SpringUtil.getBean("userManager");
    }
    return userManager;
  }
  /**
   * 保存业务对象的函数.
   */
  public String doSaveEntity(UserInfo object) {
    return (String) getHibernateTemplate().save(object);
  }
  public void doUpdateEntity(UserInfo object) {
    this.getHibernateTemplate().update(object);
  }
  public UserInfo getUserInfoById(String id){
    if(id == null || id.equals("")){
      return null;
    }
    return (UserInfo)this.find("from UserInfo where id = '"+id+"'");
  }
  public UserInfo getuserInfoByLoginName(String loginname,String password){
    if(loginname == null || loginname.equals("")
    || password == null || password.equals("")){
      return null;
    }
    String hql = "from UserInfo WHERE loginname = '"+loginname+"' AND password = '"+password+"' ";
    return (UserInfo)this.find(hql);
  }
  public UserInfo getuserInfoByLoginName(String loginname){
    if(loginname == null || loginname.equals("")){
      return null;
    }
    String hql = "from UserInfo WHERE loginname = '"+loginname+"'";
    return (UserInfo)this.find(hql);
  }
  /**
   *修改密码
   */
  public int  changepassword(String loginname,String newpassword){
    UserInfo ui = this.getuserInfoByLoginName(loginname);
    if(ui != null){
      ui.setPassword(newpassword);
      this.getHibernateTemplate().update(ui);
      return 1;
    }else{
      return -1;
    }
  }
  public List getUserListByOrgid(String orgid){
    
    String hql = "from UserInfo where 1=1 ";
    
    if(orgid != null && !orgid.equals("")){
      hql +=" and orgInfo.id = '"+orgid+"'";
    }else{
      hql +=" and orgInfo.id is null ";
    }
    return this.getHibernateTemplate().find(hql);
  }
  /**
   *初始化操作
   *初始化用户的信息
   *
   */
  public void initUserInfo(){
    String hql = "from OrgInfo";
    List list =  this.getHibernateTemplate().find(hql);
    if(list == null || list.size() <= 0){
      OrgManager om = OrgManager.getInstance();
      ChatUserInfoManager cuim =  ChatUserInfoManager.getInstance();
      for (int i = 0; i < 4; i++) {
        String orgname = "";
        if(i == 1 ){
          orgname = "技术部";
        }else if(i == 2 ){
          orgname = "销售部";
        }else if(i == 3 ){
          orgname = "行政部";
        }else if(i == 0 ){
          orgname = "财务部";
        }
        OrgInfo oi = new OrgInfo();
        oi.setChildren("0");
        oi.setName(orgname);
        oi.setParentid("");
        String orgid = om.doSaveEntity(oi);
        for (int k = 0; k < 3; k++) {
          UserInfo object = new UserInfo();
          object.setName("test"+k+i);
          object.setLoginname("test"+k+i);
          object.setPassword("1234");
          object.setOrgInfo(om.getOrgInfoById(orgid));
          String userid = userManager.doSaveEntity(object);
          //保存进聊天系统中
          ChatUserInfo cui = new ChatUserInfo();
          cui.setUserid(userid);
          cuim.doSaveEntity(cui);
        }
      }
    }
  }
  /**
   *覆盖父类的方法
   */
  public  Object find(String hql){
    List list =  this.getHibernateTemplate().find(hql);
    if(list != null && list.size() >0 ){
      return list.get(0);
    }else{
      return null;
    }
  }
}
